/*
 * @lc app=leetcode.cn id=67 lang=java
 *
 * [67] 二进制求和
 */

// @lc code=start
class Solution {
    public String addBinary(String a, String b) {
        int[] a1=new int[Math.max(a.length(), b.length())];
        int[] b1=new int[Math.max(a.length(), b.length())];
        char[] a_array=a.toCharArray(),b_array=b.toCharArray();
        for (int i = 0; i < a_array.length; i++) {
            a1[a.length()-i-1]=a_array[i]-'0';
        }
        for (int i = 0; i < b_array.length; i++) {
            b1[b.length()-1-i]=b_array[i]-'0';
        }
        StringBuilder stringBuilder=new StringBuilder();
        int middle=0;
        for (int i = 0; i < a1.length; i++) {
            a1[i]+=middle+b1[i];
            middle=a1[i]/2;
            
            stringBuilder.append(a1[i]%2);
        }
        if (middle!=0) {
            stringBuilder.append(middle);
        }
        return stringBuilder.reverse().toString();
    }
}
// @lc code=end

